﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.NetworkInformation;
using System.Net;

namespace Localizer
{
    public partial class RXSetter : UserControl
    {
        #region Member Variables
        private int _number;
        
        private double _xOffset;
        private double _yOffset;
        private double _zOffset;

        private int _port;
        private IPAddress _ipAddress;
        private String _defaultIpAddress;

        private Ping pingSender;
        #endregion

        #region Constructor
        public RXSetter()
        {
            InitializeComponent();
            _defaultIpAddress = "0.0.0.0";
            _port = 2000;
            _number = 0;
            _xOffset = 0;
            _yOffset = 0;
            _zOffset = 0;

            pingTimer.Start();
        }
        #endregion

        #region Event Handlers
        private void RXSetter_Load(object sender, EventArgs e)
        {
            rxNumber.Text = _number.ToString();
            
            ipBox.Text = _defaultIpAddress;
            
            xBox.Text = _xOffset.ToString();
            yBox.Text = _yOffset.ToString();
            zBox.Text = _zOffset.ToString();
            
            IPAddress.TryParse(ipBox.Text, out _ipAddress);
        }

        private void pingTimer_Tick(object sender, EventArgs e)
        {
            if (rxEnabledBox.Checked)
            {
                pingSender = new Ping();
                pingSender.PingCompleted += new PingCompletedEventHandler(PingCompletedCallback);
                pingSender.SendAsync(_ipAddress, 500, Encoding.ASCII.GetBytes("<PING>"),new PingOptions(2, true), null);
            }
        }
      
        private void ipBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                checkIP();
        }

        private void ipBox_Validating(object sender, CancelEventArgs e)
        {
            checkIP();
        }

        private void xBox_Validating(object sender, CancelEventArgs e)
        {
            _xOffset = checkOffset(xBox);
        }

        private void xBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                _xOffset = checkOffset(xBox);
        }

        private void yBox_Validating(object sender, CancelEventArgs e)
        {
            _yOffset = checkOffset(yBox);
        }

        private void yBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                _yOffset = checkOffset(yBox);
        }

        private void zBox_Validating(object sender, CancelEventArgs e)
        {
            _zOffset = checkOffset(zBox);
        }

        private void zBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                _zOffset = checkOffset(zBox);
        }

        private void rxEnabledBox_CheckedChanged(object sender, EventArgs e)
        {
            if (!rxEnabledBox.Checked)
                rxLabel.BackColor = Color.White;
        }

        private void PingCompletedCallback(object sender, PingCompletedEventArgs e)
        {
            if (e.Reply.Status == IPStatus.Success)
            {
                if (e.Reply.RoundtripTime < 100)
                    rxLabel.BackColor = Color.Green;
                if (e.Reply.RoundtripTime >= 100)
                    rxLabel.BackColor = Color.Yellow;
            }

            if (e.Reply.Status == IPStatus.TimedOut || e.Reply.Status == IPStatus.DestinationHostUnreachable)
            {
                rxLabel.BackColor = Color.Red;
            }

            ((IDisposable)sender).Dispose();
        }

        #endregion

        #region Private Functions
        private double checkOffset(TextBox value)
        {
            double offset = 0;
            if (!Double.TryParse(value.Text, out offset))
            {
                MessageBox.Show("D'Oh! \"" + value.Text + "\" is an invalid offset.", "Stupid. 0_0");
                value.Text = "0";
            }
            return offset;
        }

        private void checkIP()
        {
            String ip = ipBox.Text;
            String pattern = @"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b";
            Boolean ipPatternMatch = System.Text.RegularExpressions.Regex.IsMatch(
                ip,
                pattern,
                System.Text.RegularExpressions.RegexOptions.IgnoreCase
                );

            if (!IPAddress.TryParse(ip, out _ipAddress) || !ipPatternMatch)
            {
                MessageBox.Show("D'Oh! \"" + ip + "\" is an invalid IP Address.", "Idiot. >=[");

                ipBox.Text = _defaultIpAddress;
                IPAddress.TryParse(ipBox.Text, out _ipAddress);
            }
            System.Console.WriteLine(_ipAddress.ToString());
        }
        #endregion

        #region Getter-Setter Methods
        public string DefaultIPAddress
        {
            set { this._defaultIpAddress = value; } 
            get { return this._defaultIpAddress; }
        }

        public int RXNumber
        {
            set { this._number = value; } 
            get { return this._number; }
        }

        public IPAddress IpAddress
        {
            get { return this._ipAddress; }
        }

        #endregion
    }
}
